﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.Code.UserControls
{
    public partial class UCDuLieuDetail : System.Web.UI.UserControl
    {
        DataClassesDataContext db = new DataClassesDataContext();
        CHCommonCode itemCommon = new CHCommonCode();
        CommonCode common = new CommonCode();
        List<CHCommonCode> lstCommonCode;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void ShowPopUp_DuLieuDetail(string type, string codeKey)
        {
            hdType.Text = type;
            hdLoaiMa.Text = codeKey;
            txtLoaiMa.Text = codeKey;
            if (type.Equals("View"))
            {
                btnSave.Hidden = true;
                btnAddNew.Disabled = true;
                LoadDataGrid(codeKey);
            }
            else if (type.Equals("AddNew"))
            {
                btnSave.Hidden = false;
                btnAddNew.Disabled = false;
                storeDulieuDetail.RemoveAll();
                storeDulieuDetail.DataBind();
                txtGhiChu.Text = string.Empty;
                txtLoaiMa.Text = string.Empty;
            }
            else
            {
                btnSave.Hidden = false;
                btnAddNew.Disabled = false;
                LoadDataGrid(codeKey);
            }
            wdDuLieuDetail.Show();
        }
        void LoadDataGrid(string codeKey)
        {
            storeDulieuDetail.DataSource = common.LoadDataCommonByCodeKey(codeKey);
            storeDulieuDetail.DataBind();
            Session["CHCommonCode"] = common.LoadDataCommonByCodeKey(codeKey);
        }
        [DirectMethod]
        public string CheckTonTai()
        {
            CHCommonCode item = db.CHCommonCodes.Where(a => a.CodeKey.Equals(common.LoaiMa) && a.CodeValue.Equals(txtLoaiMa.Text.Trim())).SingleOrDefault();
            if (item != null)
            {
                return "true";
            }
            return "false";
        }
        [DirectMethod]
        public void ShowDialog(string type, string codeId)
        {

            if (type.Equals("Delete"))
            {
                itemCommon.DeleteCommonCode(Convert.ToInt32(codeId));
            }
            else
            {
                AddCommonCode.ShowPopUp_DetailCommonCode(type, codeId, txtLoaiMa.Text);
            }
            X.Mask.Hide();
        }
        public void BindGrid(string codeKey)
        {
            storeDulieuDetail.DataSource = common.LoadDataCommonByCodeKey(codeKey);
            storeDulieuDetail.DataBind();
        }
        public void GetCommonCode(CHCommonCode hdld)
        {

            List<CHCommonCode> lst = (List<CHCommonCode>)Session["CHCommonCode"];
            if (lst == null)
            {
                lst = new List<CHCommonCode>();
            }
            lst.Add(hdld);
            Session["CHCommonCode"] = lst;
            storeDulieuDetail.DataSource = lst;
            storeDulieuDetail.DataBind();
        }
        public void ReloadGrid(string type)
        {

            List<CHCommonCode> lst = (List<CHCommonCode>)Session[type];
            storeDulieuDetail.DataSource = lst;
            storeDulieuDetail.DataBind();

        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            lstCommonCode = (List<CHCommonCode>)Session["CHCommonCode"];
            if (hdType.Text.Equals("AddNew"))
            {
                CHCommonCode item = db.CHCommonCodes.Where(a => a.CodeKey.Equals(common.LoaiMa) && a.CodeValue.Equals(txtLoaiMa.Text.Trim())).SingleOrDefault();
                if (item != null)
                {
                    X.MessageBox.Alert("Thông báo", "Loại mã này đã tồn tại.").Show();
                    txtLoaiMa.Focus();
                }
                else
                {
                    InsertListCommonCode(lstCommonCode);
                    ((HeThongDuLieu)this.Page).LoadData();
                    wdDuLieuDetail.Hide();
                }
            }
            else
            {
                itemCommon.UpdateCommonCode(lstCommonCode);
                ((HeThongDuLieu)this.Page).LoadData();
                wdDuLieuDetail.Hide();
            }
            X.Mask.Hide();
          
        }

        void InsertListCommonCode(List<CHCommonCode> lst)
        {
            lst = new List<CHCommonCode>();
            CHCommonCode item = new CHCommonCode();
            item.CodeValue = txtLoaiMa.Text;
            item.GhiChu = txtGhiChu.Text;
            item.CodeKey = common.LoaiMa;
            lst.Add(item);
            db.CHCommonCodes.InsertAllOnSubmit(lst);
            db.SubmitChanges();
        }
    }
}